package com.hongfeng.support.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.AutoBrand;
import com.hongfeng.support.service.AutoBrandService;
import com.hongfeng.support.util.config.PropertiesUtil;
import com.hongfeng.support.util.file.FileUploadUtil;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 汽车品牌Controller
 * 
 * @author llc
 * 
 */
@Controller
@Online
public class AutoBrandController extends BaseController {

	@Resource
	private AutoBrandService autoBrandService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String brandName, Page<AutoBrand> page) {
		log.info("查询品牌名：" + brandName);
		ParmItem item = new ParmItem("name", brandName, FieldOperator.LIKE);
		autoBrandService.findPageByQuery(page, AutoBrand.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("listAll")
	public void listAll(HttpServletRequest request, HttpServletResponse response) {
		List<AutoBrand> list = autoBrandService.findAll();
		String json = JsonUtils.array2Json(list);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("brand") String brandJson) {
		log.info("添加品牌:" + (brandJson != null ? brandJson : null));
		if (brandJson == null) {
			this.sendErrorResponse(response, "品牌信息不能为空。");
			return;
		}
		AutoBrand brand = (AutoBrand) JsonUtils.json2Object(brandJson,
				AutoBrand.class);
		ParmItem item = new ParmItem("name", brand.getName(),
				FieldOperator.EQUAL);
		List<AutoBrand> brands = autoBrandService.findAllByQuery(
				AutoBrand.class, item);
		if (brands.size() == 0) {
			autoBrandService.create(brand);
		} else {
			this.sendErrorResponse(response, "品牌名已经存在。");
		}
	}

	@RequestMapping("upload")
	public void upload(HttpServletRequest request, HttpServletResponse response) {
		String base = "autobrand";
		String webUrl = PropertiesUtil.getConfig("hongfeng.web.url");
		try {
			List<String> files = FileUploadUtil.fileUpload(request, base);
			String fileUrl = "";
			for (String file : files) {
				fileUrl = webUrl + file;
				log.info("fileUrl = " + fileUrl);
			}
			this.sendSuccResponse(response, fileUrl);
		} catch (Exception e) {
			log.error("上传图片失败", e);
			this.sendErrorResponse(response, "上传图片失败。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("brand") String brandJson) {
		log.info("修改品牌:" + (brandJson != null ? brandJson : null));
		if (brandJson == null) {
			this.sendErrorResponse(response, "品牌信息不能为空。");
			return;
		}
		AutoBrand brand = (AutoBrand) JsonUtils.json2Object(brandJson,
				AutoBrand.class);
		AutoBrand qBrand = autoBrandService.read(brand.getId());
		List<AutoBrand> brands = null;
		if (!brand.getName().equals(qBrand.getName())) {
			ParmItem item = new ParmItem("name", brand.getName(),
					FieldOperator.EQUAL);
			brands = autoBrandService.findAllByQuery(AutoBrand.class, item);
		}
		if (brands == null || brands.size() == 0) {
			brand.setCreateTime(qBrand.getCreateTime());
			brand.setCreator(qBrand.getCreator());
			autoBrandService.update(brand);
		} else {
			this.sendErrorResponse(response, "品牌名已经存在。");
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取品牌:" + id);
		AutoBrand brand = autoBrandService.read(id);
		String json = JsonUtils.bean2Json(brand);
		log.info("品牌信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除品牌:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的品牌。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			autoBrandService.delete(Long.parseLong(id));
		}
	}

}